import Vue from 'vue'
import VueCordova from 'vue-cordova'
import FastClick from 'fastclick'
import App from './App'
import router from './router'
import store from './store/'
import lodash from 'lodash'
import VueLodash from 'vue-lodash'

FastClick.attach(document.body)

Vue.use(VueCordova)

Vue.use(VueLodash, lodash)

Vue.config.productionTip = false

window._ = Vue._

const history = window.sessionStorage;
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)

router.beforeEach(function(to, from, next) {
  // store.commit('updateLoadingStatus', { isLoading: true })
  const toIndex = history.getItem(to.path);
  const fromIndex = history.getItem(from.path);

  if (toIndex) {
    if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
      store.commit('UPDATE_DIRECTION', { direction: 'forward' })
    } else {
      store.commit('UPDATE_DIRECTION', { direction: 'reverse' })
    }
  } else {
    ++historyCount;
    history.setItem('count', historyCount);
    (to.path !== '/') && history.setItem(to.path, historyCount);
    store.commit('UPDATE_DIRECTION', { direction: 'forward' })
  }
  // 判断有没有登录
  const loginUser = JSON.parse(localStorage.getItem('user')) || {}
  if (!loginUser.userName && to.path !== '/login') {
    router.push({ path: '/login' })
  } else {
    next()
  }
})

router.afterEach(function(to) {
  // store.commit('updateLoadingStatus', { isLoading: false })
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app-box')
